@extends('layouts.admin')

@section('title', '家庭联系-添加')

@section('content')
    <article class="page-container">
        <table id="listTable" class="table table-border table-bordered table-bg table-hover">
            <thead>
            <tr class="text-c">
                <th><input type="checkbox" name="checkList"/></th>
                <th>与学生关系</th>
                <th>姓名</th>
                <th>电话</th>
                <th>工作单位</th>
            </tr>
            </thead>
        </table>
    </article>
@endsection
@section('script')
    <script>
        var $dataTable=null;
        var $MAIN={currentPage:1}
        $(function(){
            $dataTable=$('#listTable').dataTable({
                "sPaginationType" : "full_numbers",
                "processing": true,
                "serverSide": true,
                "searching": false,
                "ordering": false,
                "dom": '<l<\'#topPlugin\'>f>rt<ip><"clear">',
                "ajax":{
                    "url":"{{url('student/relation/list')}}",
                    "type": 'post',
                    "data": function ( d ) {
                        d.student_id='{{$student_id}}';
                        d.page=$MAIN.currentPage
                    },
                    'dataSrc':function(result){
                        var datas=result.data;
                        if(datas){
                            for(var i=0;i<datas.length;i++){
                                datas[i].DT_RowClass='text-c';
                            }
                        }
                        return datas;
                    }
                },
                "columnDefs": [{
                    "targets": "_all",
                    "searchable":false
                }],
                "columns": [
                    {
                        "data": "id",
                        "render": function (data, type, row, meta) {
                            return '<input type="checkbox"  name="checkList" class="checkchild"  value="' + data + '" />';
                        }
                    },
                    { "data": "relation_name"},
                    { "data": "name" },
                    { "data": "phone" },
                    { "data": "company" },
                ],
                drawCallback: function( settings ) {
                    $('input[name=checkList]')[0].checked=false;
                },
                "initComplete":function(settings, json){
                    var topPlugin='<button class="btn btn-danger radius delSelectBtn"><i class="Hui-iconfont">&#xe6e2;</i> 批量删除</button> ' +
                        '<button class="btn btn-primary radius addBtn" ><i class="Hui-iconfont">&#xe600;</i>新 增</button>';
//                                  '<button  class="btn btn-info  btn-sm radius expCsv">导出全部</button><iframe id="exp" style="display:none;"></iframe>'
                    $("#topPlugin").append(topPlugin);
                }
            });
            $dataTable.on( 'page.dt', function () {
                var info = $dataTable.api();
                $MAIN.currentPage=parseInt(info.page())+1;
            } );
        });

        $MAIN.search=function(){
            $dataTable.api().ajax.reload();
        }
        $MAIN.getSelectCheckBoxValue=function(){
            var theArray=[];
            $("input[name=checkList]:checked").each(function() {
                theArray.push($(this).val());
            });
            return theArray;
        }
        $MAIN.deleteDatas=function(ids){
            layer.confirm('确定要删除选择的数据？',{
                    btn: ['确定','取消'],
                    shade: false,
                    closeBtn: 0
                },
                function(){
                    $.ajax({
                        url: "{{url('student/relation/delete')}}",
                        data:{ids:ids},
                        dataType: 'json',
                        success: function(data){
                            if(data['successFlag']==1){
                                layer_success_message('删除成功!');
                                $MAIN.search();
                            }
                        },
                        error:function(data) {
                            layer_error_message('删除失败!');
                        },
                    });
                });
        }

        $(function(){
            $(".btnSearch").click(function(){
                $MAIN.search();
            });
            $(document).on("click",".delSelectBtn",function(){
                var ids=$MAIN.getSelectCheckBoxValue();
                if(ids.length<=0){
                    layer.msg('请选择需要删除的数据', {icon:5,time:1000});
                }else{
                    $MAIN.deleteDatas(ids);
                }
            });
            $(document).on("click",".addBtn",function(){
                layer_show("添加家庭关系",'{{url('student/relation/add')}}?student_id={{$student_id}}',800,400);
            });
            $(document).on("click",".delBtn",function(){
                $MAIN.deleteDatas([$(this).data("id")]);
            });
        })
    </script>
@endsection